package com.fansl.allround.ucenter.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.fansl.allround.common.core.bean.UserInfo;
import com.fansl.allround.common.core.constant.enums.OauthTypeEnum;
import com.fansl.allround.common.core.exception.CheckedException;
import com.fansl.allround.common.core.validator.NotNullValidator;
import com.fansl.allround.common.security.annotation.Inner;
import com.fansl.allround.ucenter.api.common.constant.UcenterResult;
import com.fansl.allround.ucenter.api.common.constant.UcenterResultEnum;
import com.fansl.allround.ucenter.api.entity.UcenterUser;
import com.fansl.allround.ucenter.api.vo.UcenterUserOauthVO;
import com.fansl.allround.ucenter.service.UcenterUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author fansl
 * @Description: TODO
 * @date 2019/9/20 15:48
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UcenterUserController {
    private final UcenterUserService ucenterUserService;

    /**
     * 获取用户中心信息
     * @param oauthName 认证信息
     * @param ucenterOauthType 认证类型
     * @return
     */
    @Inner
    @GetMapping("/info")
    public UserInfo info(@RequestParam("oauthName") String oauthName,
                         @RequestParam("oauthType") OauthTypeEnum ucenterOauthType){
        UcenterUserOauthVO ucenterUserOauthVO =
                ucenterUserService.getByOauthType(oauthName,ucenterOauthType);
        return ucenterUserService.getUserInfo(ucenterUserOauthVO);
    }

    /**
     * 创建用户信息（带认证信息）
     * @param ucenterUser 用户信息
     * @param oauthValue 认证信息
     * @param ucenterOauthType 认证类型
     * @return
     */
    @Inner
    @PostMapping("/create")
    public UserInfo create(@RequestBody UcenterUser ucenterUser,
                           @RequestParam("oauthValue")String oauthValue,
                           @RequestParam("oauthType")OauthTypeEnum ucenterOauthType){
        ComplexResult result = FluentValidator.checkAll()
                .on(ucenterUser.getNickname(),new NotNullValidator("昵称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if(!result.isSuccess()){
            throw new CheckedException(result.toString());
        }
        return ucenterUserService.create(ucenterUser,oauthValue,ucenterOauthType);
    }

    /**
     * 完善用户信息
     * @return
     */
    @PostMapping("/improve")
    public UcenterResult improveUserInfo(){
        return new UcenterResult(UcenterResultEnum.SUCCESS,Boolean.TRUE);
    }
}
